﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using uban.demo.Protocol;

namespace uban.demo
{
    public partial class MainForm : Form
    {
        Server server;
        public MainForm()
        {
            InitializeComponent();
        }

        
        private void btnStart_Click(object sender, EventArgs e)
        {
            int port = 8080;
            int.TryParse(txtPort.Text, out port);
            server = new Server(port);
            if (server.Start())
            {
                btnStart.Enabled = false;
                btnStop.Enabled = true;
                btnSendSchool.Enabled = true;
                btnSendName.Enabled = true;
                server.OnConnet += Server_OnConnet;
                server.OnLogin += Server_OnLogin;
                server.OnKeyInfo += Server_OnKeyInfo;
                server.OnPenInput += Server_OnPenInput;
                server.OnException += Server_OnException;
            }
            else
            {
                MessageBox.Show("启动失败！");
            }
        }

        private void Server_OnConnet(System.Net.EndPoint obj)
        {
            Console.WriteLine($"设备端口地址：{obj}！");
        }

        private void Server_OnException(Exception obj)
        {
            Console.WriteLine(obj.Message);
        }
        int max_x = 14800;
        int max_y = 21000;
        Point previousPoint = Point.Empty;
        int previousPressure = 0;
        private void Server_OnPenInput(PenInputState obj)
        {
            Console.WriteLine($"PenInput：{obj}！");
            if (obj.Pressure > 0)
            {
                short x = (short)(obj.X * img.Width * 1.0f / max_x);
                short y = (short)(obj.Y * img.Height * 1.0f / max_y);
                Point point = new Point(x, y);
                if (obj.Pressure > 0 && previousPressure > 0)
                {
                    Graphics g = img.CreateGraphics();
                    g.DrawLine(new Pen(Color.White, 2), previousPoint, point);
                }
                previousPoint = point;
            }
            previousPressure = obj.Pressure;
        }

        private void Server_OnKeyInfo(KeyStatue obj)
        {
            Console.WriteLine($"你输入了{obj}！");
        }

        private void Server_OnLogin(LoginState obj)
        {
            Console.WriteLine($"设备{obj.SerialId}登录成功！");
            server.SendSchool(txtSchool.Text);
            server.SenStudentInfo(txtName.Text, txtNo.Text);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnSendSchool.Enabled = false;
            btnSendName.Enabled = false;
            server.Stop();
            server = null;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Graphics g = img.CreateGraphics();
            g.Clear(Color.Black);
        }

        private void btnSendSchool_Click(object sender, EventArgs e)
        {
            server.SendSchool(txtSchool.Text);
        }

        private void btnSendName_Click(object sender, EventArgs e)
        {
            server.SenStudentInfo(txtName.Text, txtNo.Text);
        }
    }
}
